package ar.com.ford.it.fullEconomics.interfaces.threadExecutor;

import ar.com.ford.it.fullEconomics.interfaces.CommonInterfaceLauncher;
import ar.com.ford.it.fullEconomics.interfaces.interfaz.InterfaceProccesFile;
import ar.com.ford.it.fullEconomics.interfaces.strategys.PartStrategy;
import commonj.timers.Timer;
import commonj.timers.TimerListener;

public class TimerInterfaceWipsParts implements TimerListener {

	public String toString()
	{
		return this.getProcessName();
	}
	
	private String getProcessName()
	{
		return "TimerInterfaceWipsParts";
	}
	
	
	public void timerExpired(Timer arg0) {
		
		System.out.println("+++ Ejecutando: " + this.toString() + " desde TimerManager");
		
		InterfaceProccesFile inStrategy = (InterfaceProccesFile) new PartStrategy();
		CommonInterfaceLauncher launcher = new CommonInterfaceLauncher();  
		launcher.processPendingInterfaceFiles(5, inStrategy, this.getProcessName(), "p_feefmc","B");
		
		/*
		LogProcessLogic logProcess = new LogProcessLogic();
        String userId = "p_feefmc";
        Teesx03Intflog intflog = null;
        ReadFile parser;
        Teesx01Intfsource intfSource = new Teesx01Intfsource();
        intfSource.setEesx01IntfIdNumberK(5);
        
        try {
            
            //Teesx02Fileconten fileConten = logProcess.getFileContent(intfSource);

            Collection<Teesx02Fileconten> pendFiles = null;
            
            pendFiles = logProcess.getFileContentsByStatus("P", intfSource, null, null);
            
            for (Iterator iterFiles = pendFiles.iterator(); iterFiles.hasNext();) {
        
            	Teesx02Fileconten fileConten = (Teesx02Fileconten) iterFiles.next();
	    
            	//---------------
            	
            	parser = new ReadFile(fileConten,this.getProcessName());
	            // Estrategia de lecutra
	            InterfaceProccesFile inStrategy = (InterfaceProccesFile) new PartStrategy();
	
	            Teesx03Intflog lastIntfLog = logProcess.getLastIntfLog(fileConten
	                    .getEesx01Intfsource());
	            if (lastIntfLog != null
	                    && lastIntfLog.getEesx03IlogProcStatusCodeC()
	                            .equalsIgnoreCase("R")) {
	                long diffInHours = FullEconomicsDateUtil.getDifferenceInHours(
	                        lastIntfLog.getEesx03IlogStartM(), new Timestamp(System
	                                .currentTimeMillis()));
	                // si es mayor a 6 horas, lo cierro y abro el nuevo
	                if (diffInHours > 6) {
	                    logProcess
	                            .insertLogIntfLevent(
	                                    userId,
	                                    lastIntfLog,
	                                    "E",
	                                    "Se cancelo el proceso, tiempo de corrida mayor a 6 horas",
	                                    0);
	                    logProcess.updateLogInfo(lastIntfLog, "C", new Timestamp(
	                            System.currentTimeMillis()));
	
	                }else{
	                	
	                	// Aviso de que hubo un intento de reprocesar algo dentro de las 6 horas de inicio.
	                	
	                	logProcess
                        .insertLogIntfLevent(
                                userId,
                                lastIntfLog,
                                "W",
                                "Hubo un intento de volver a procesar la ejecucion en curso",
                                0);
	
	                }
	            } else {
	            	// Si el ultimo archivo dio "S" 
	            	// o no hay registros de ejecucion para este archivo
	            	
	                // Status Proces Running
	                intflog = logProcess.insertLogInf(intfSource, fileConten, userId, "R",
	                        new Timestamp(System.currentTimeMillis()));
	                parser.processLineByArray(inStrategy, intflog, userId);
	                logProcess.updateLogInfo(intflog, "S", new Timestamp(System
	                        .currentTimeMillis()));
	
	            }
            	
            	//---------------
            
            } // for iterFiles
            
        } catch (FullEconomicsException e) {
            logProcess.updateLogInfo(intflog, "C", new Timestamp(System
                    .currentTimeMillis()));

        } catch (FileNotFoundInDatabaseException fnf) {
       		System.out.println(fnf.getMessage());
       		logProcess.updateLogInfo(intflog, "C", new Timestamp(System
                    .currentTimeMillis()));
        }
        finally {
        */
            System.out.println("+++ finalizando: " + this.toString() + " desde TimerManager");
        //}

    }


	
}